package com.wwwc.index.web.servlet;

import java.io.*;
import java.awt.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.ServletRequestWrapper;
import java.net.*;

import javax.ejb.*;
import javax.naming.*;
import java.rmi.*;
import javax.rmi.*;

import com.wwwc.util.web.*;
import com.wwwc.index.web.ejb.database.*;

public class IndexServlet extends HttpServlet {
    private EJBDatabaseQuery ejbdb;

    public void doPost(HttpServletRequest request,  HttpServletResponse response) 
    {
	//String default_mid = "27";
	String default_mid = "27";
	long t0 = System.currentTimeMillis();
	try {
	    response.setContentType("text/html");
	    HttpSession session = request.getSession(true);
	    PrintWriter out     = response.getWriter();
	    String user_ip      = request.getRemoteAddr();

	    String id  = request.getParameter("id");
	    String mid = request.getParameter("mid");
	    String user_name  = "guest";
	    String user_age   = "0";
	    String user_level = "0";
	    String call_path  = request.getContextPath()+request.getServletPath();

	    Hashtable user_info = (Hashtable)session.getAttribute("user_info");
	    String screen_w     = null;
	    String screen_h     = null;

	    if(user_info == null) {
		user_info = new Hashtable();
		user_info.put("user_name","guest");
		user_info.put("user_age","0");
		user_info.put("user_level","0");
		user_info.put("user_fname","n/a");
		user_info.put("user_lname","n/a");
		user_info.put("user_since","n/a");
		user_info.put("user_edu","n/a");
		user_info.put("user_zip","0");
		user_info.put("user_sex","n/a");
		user_info.put("user_ip",user_ip);

		screen_w = request.getParameter("sw");
		screen_h = request.getParameter("sh");
		
		if(screen_w != null && screen_h != null) {
		    user_info.put("screen_w", screen_w);
		    user_info.put("screen_h", screen_h);
		}
		else {
		    user_info.put("screen_w", "1280");
		    user_info.put("screen_h", "1024");
		}
	    }
	    else {
		user_name   = (String)user_info.get("user_name");
		user_level  = (String)user_info.get("user_level");
		user_age    = (String)user_info.get("user_age");
		screen_w    = (String)user_info.get("screen_w");
		screen_h    = (String)user_info.get("screen_h");
	    }

	    //=================================================================
	    // change page color setting
	    //=================================================================

	    String page_color_id = request.getParameter("pc_id");
	    if(page_color_id != null) {
		session.setAttribute("page_color",((new IndexBean()).getPageColor(page_color_id)));
		Cookie pc_cookie = new Cookie("page_color",page_color_id);
		pc_cookie.setMaxAge(3600*24*365);
		pc_cookie.setValue(page_color_id);
		pc_cookie.setPath(call_path);
		response.addCookie(pc_cookie);
	    }

	    //==================================================================
	    // set cookie or get cookie info
	    //==================================================================

	    Cookie cookies[] = request.getCookies();
	    if (cookies == null || cookies.length == 0) {
		/*
		Cookie rate_cookie = new Cookie("3wcenter","0000");
		rate_cookie.setMaxAge(3600);
		rate_cookie.setValue(session.getId());
		rate_cookie.setComment("cookie is required.");
		rate_cookie.setPath("/index/i");
		response.addCookie(rate_cookie);
		*/

		Cookie pc_cookie = new Cookie("page_color","5");
		pc_cookie.setMaxAge(3600*24*365);
		pc_cookie.setPath(call_path);
		response.addCookie(pc_cookie);
		//cookies[0] = pc_cookie;
		//System.out.println("Set Cookies");
	    }
	    else {
		//System.out.println("Get Cooike info");
		String ck_name  = null;
		String ck_value = null;
		for(int i=0; i< cookies.length; i++) {
		    ck_name  = cookies[i].getName();
		    ck_value = cookies[i].getValue();
		    if(ck_name.equals("page_color")) {
			page_color_id = ck_value;
			request.setAttribute("page_color_id",page_color_id);
		    }
		    //System.out.println(ck_name+"="+ck_value);
		}
	    }

	    if(session.getAttribute("page_color") == null) {
		session.setAttribute("page_color",((new IndexBean()).getPageColor(page_color_id)));
	    }

	    /////////////////////////////////////////////////////////////////////
	    // Side bar setting
	    /////////////////////////////////////////////////////////////////////

	    Hashtable coc = (Hashtable)session.getAttribute("ContralOpenClose");
	    Vector keys   = (Vector)session.getAttribute("SideBarKeys");
	    String reload = request.getParameter("ReloadBar");
	    Vector nkeys = null;

	    if(coc == null || keys == null || reload != null) {		
		id  = null;
		if(reload != null) {
		    nkeys = new Vector();
		}
		if(coc == null) {
		    coc    = new Hashtable();
		}
		if(keys == null) {
		    keys   = new Vector();
		}

		String did           = null;
		String sid           = null;
		String name          = null;
		String type          = null;
		String pid           = null;
		String mlink         = null;
		String menu_level    = null;
		String menu_age      = null;
		String preview_level = null;
		String preview_age   = null;
		String read_level    = null;
		String read_age      = null;
		String post_level    = null;
		String post_age      = null;
		String edit_level    = null;
		String remove_level  = null;
		String delete_level  = null;
		String admin_level   = null;
		String managers      = null;
		String admins        = null;

		ArrayList alist = new ArrayList();
		if (ejbdb == null) {
		    ejbdb = (new DatabaseEJBQuery()).getEJBDatabaseQuery();
		}

		String query = "SELECT * FROM MAIN_MENU WHERE MENU_LEVEL <= "+user_level+" ORDER BY SID";

		try {
		    alist = ejbdb.select(query);
		} 
		catch (Exception e) {
		    System.out.println("IndexServlet:Error:101:"+ e);
		}

		Iterator rit = alist.iterator();
		Iterator sit = null;

		while (rit.hasNext()) {
		    sit = ((ArrayList)rit.next()).iterator();
		    while (sit.hasNext()) {
			did           = (String)sit.next();
			sid           = (String)sit.next();
			name          = (String)sit.next();
			type          = (String)sit.next();
			pid           = (String)sit.next();
			mlink         = (String)sit.next();
			menu_level    = (String)sit.next();
			menu_age      = (String)sit.next();
			preview_level = (String)sit.next();
			preview_age   = (String)sit.next();
			read_level    = (String)sit.next();
			read_age      = (String)sit.next();
			post_level    = (String)sit.next();
			post_age      = (String)sit.next();
			edit_level    = (String)sit.next();
			remove_level  = (String)sit.next();
			delete_level  = (String)sit.next();
			admin_level   = (String)sit.next();
			managers      = (String)sit.next();
			admins        = (String)sit.next();

			if(reload != null) {
			    nkeys.add(did);
			}
			if(keys.contains(did)) {
			    continue;
			}

			keys.add(did);	 
			if(name != null && name.equals("$User")) {
			    name = (user_name.substring(0,1)).toUpperCase()+user_name.substring(1)+"'s";
			}
			if(managers != null && managers.equals("$User")) {
			    managers = user_name;
			}

			if(sid != null)
			    coc.put(did+"_sid",sid);
			if(name != null)
			    coc.put(did+"_name",name);
			if(type != null)
			    coc.put(did+"_type",type);
			if(pid != null)
			    coc.put(did+"_pid",pid);
			if(mlink != null)
			    coc.put(did+"_mlink",mlink);
			if(menu_level != null)
			    coc.put(did+"_menu_level",menu_level);
			if(menu_age != null)
			    coc.put(did+"_menu_age",menu_age);
			if(preview_level != null)
			    coc.put(did+"_preview_level",preview_level);
			if(preview_age != null)
			    coc.put(did+"_preview_age",preview_age);
			if(read_level != null)
			    coc.put(did+"_read_level",read_level);
			if(read_age != null)
			    coc.put(did+"_read_age",read_age);
			if(post_level != null)
			    coc.put(did+"_post_level",post_level);
			if(post_age != null)
			    coc.put(did+"_post_age",post_age);
			if(edit_level != null)
			    coc.put(did+"_edit_level",edit_level);
			if(remove_level != null)
			    coc.put(did+"_remove_level",remove_level);
			if(delete_level != null)
			    coc.put(did+"_delete_level",delete_level);
			if(admin_level != null)
			    coc.put(did+"_admin_level",admin_level);
			if(managers != null)
			    coc.put(did+"_managers",managers);
			if(admins != null)
			    coc.put(did+"_admins",admins);
			
			if(sid.equals(pid)) {
			    coc.put(did+"_pstatus","open");
			    coc.put(did+"_show", "true");

			    if(type.equals("0")){
				coc.put(did+"_status","close");
			    }
			    else {
				coc.put(did+"_status","open");
			    }
			}
			else {
			    coc.put(did+"_pstatus","close");
			    coc.put(did+"_status","close");
			    coc.put(did+"_show", "false");
			}
		    }
		}
		
		if(nkeys != null && reload != null) {
		    keys = nkeys;
		}
		session = request.getSession(true);
		session.setAttribute("user_info",user_info);
		session.setAttribute("ContralOpenClose",coc);
		session.setAttribute("SideBarKeys",keys);
	    }

	    //////////////////////////////////////////////////////////////////////////
	    // Servlet ID setting
	    //////////////////////////////////////////////////////////////////////////

	    if(mid == null) {
		mid = default_mid;
	    }

	    if(mid.equals("adv")) {
		mid = "71";
	    }

	    if(mid.equals("join") || mid.equals("verify")) {
		mid ="72";
	    }
	   
	    if(!keys.contains(mid)) {
		System.out.println("keys not contains mid:"+mid);
		if(!mid.equals("23")) {
		    mid = default_mid;
		}
	    }

	    request.setAttribute("mid", mid);

	    if(id != null) {
		request.setAttribute("id", id);
	    }
	    else {
		request.setAttribute("id", id);
	    }
	    
	    ////////////////////////////////////////////////////////////////////////
	    // Layout viewer
	    ////////////////////////////////////////////////////////////////////////
	    out.println(ServletBean.getStyleSheet("3WCenter.com Index","3WCenter.com, Tax Schedule-D, Enterprise Portal, Java, J2EE"));

	    String page_color = (String)session.getAttribute("page_color");
	    if(page_color == null) {
		page_color = "<BODY>";
	    }

	    out.println(page_color);
	    
	    out.println("<TABLE cellSpacing='1' cellPadding='0' border='0' width='100%' height=100%>");
	    out.println("<TR><TD colspan=2 align=center height=90>");

	    try {
		RequestDispatcher rd = request.getRequestDispatcher("/t");
		rd.include(request,response);
	    }
	    catch (ServletException e) {
		System.out.println("IndexServlet:Error:107:"+e);
		return;
	    }

	    out.println("</TD></TR>");

	    out.println("<TR><TD valign='top' style='BORDER:1px solid'>");
	    
	    try {
		RequestDispatcher rd = request.getRequestDispatcher("/l");
		rd.include(request,response);
	    }
	    catch (ServletException e) {
		System.out.println("IndexServlet:Error:107:"+e);
		return;
	    }
	    
	    out.println("</TD>");
	    out.println("<TD align='center' valign='top' style='BORDER:1px solid' width=90%>");
	    
	    try {
		RequestDispatcher rd = request.getRequestDispatcher("/m");
		rd.include(request,response);
	    }
	    catch (ServletException e) {
		System.out.println("IndexServlet:Error:108:"+e);
		return;
	    }
	    
	    out.println("</TD></TR>");
	    out.println("<TR>");
	    out.println("<TD align='center' colspan=2>");
	    out.println("<img src='/images/copy_right.gif' border='0'>");
	    out.println("</TD>");
	    out.println("</TR>");
	    out.println("</TABLE>");

	    out.println("</body>");
	    out.println("</html>");
	    return;
	}
	catch(IOException e) {
	    System.out.println("IndexServlet:Error:201:"+e);
	    return;
	}
	catch(Exception e) {
	    System.out.println("IndexServlet:Error:202:"+e);
	    return;
	}
    }

    public void doGet(HttpServletRequest request,  HttpServletResponse response)
    {
	doPost(request, response);
    }
}






